function Util(){
	this.baseUrl = 'http://api.shihou.tv/api';
	this.user_batch_url = this.baseUrl + '/user/batch?';//&user_ids[]=43177
	this.send_url = this.baseUrl + '/gift/send_rank?gift_id=';
	this.receive_url = this.baseUrl + '/gift/receive_rank?limit=100000&gift_id=';
	//礼物id
	this.gift_id = '1077';
	this.send_data_array = [];
	this.receive_data_array = [];
	
	//送礼分页相关
	this.pageSize = 10;
	this.anchorPageSize = 22;
	
	this.init();
}
Util.prototype={
	init: function(){
		var _this = this;
		//收礼榜数据
		var receive_url = _this.receive_url + _this.gift_id;
		var totalCount = 0;
		_this.requestRemoteDataJson(receive_url, {}, function(result){
			if(_this.parseResult(result, 'error') == 0){
				var resultData = _this.parseResult(result, 'data', 'list');
				var j = 0;
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					var user_id = _this.parseResult(user, 'user', 'id');
					var total = parseInt(_this.parseResult(user, 'total'));
					if(user_id == 244637 || user_id == 87)
						continue;
					totalCount += total;
					_this.receive_data_array[j] = user;
					j++;
				}
			}
		}, false);
		$('#totalCount').html(totalCount+'<em>次祈福</em>');
		_this.receive_data_array = _this.sort(_this.receive_data_array);
	},
	setUserRankList: function(){
		var _this = this;
		var html = '';
		var init = $('#phbList li').length;
		
		//送礼榜数据
		var url = _this.send_url + _this.gift_id + '&limit='+_this.pageSize+'&offset='+init;
		_this.requestRemoteDataJson(url, {}, function(result){
			if(_this.parseResult(result, 'error') == 0){
				var resultData = _this.parseResult(result, 'data', 'list');
				resultCount = resultData.length;
				for (var i = 0;i < resultCount; i++) {
					var user = resultData[i];
					_this.send_data_array[i] = user;
				}
			}
		}, false);
		
		//排行榜赋值
		for(var j = 0 ; j < _this.send_data_array.length ; j++){
			var send_data = _this.send_data_array[j];
			html += _this.setUserRankListHTML((j+1+init), _this.parseResult(send_data, 'user', 'nick_name'), _this.parseResult(send_data, 'user', 'avatar'), ''+_this.resetTotal(_this.parseResult(send_data, 'total'))+'');
		}
		if($('#phbList li').length > 0){
			$('#phbList').append(html);
			return;
		}
		$('#phbList').html(html);
		_this.send_data_array = [];
	},
	setUserRankListHTML: function(index, nick_name, avatar, value){
		var html = '';
		var em_class = 'no';
		var num = '';
		if(index < 4){
			em_class += index;
		}else{
			em_class += '4';
			num = index;
		}
		html = '<li><em class="'+em_class+'">'+num+'</em><em class="phbHd"><img src="'+avatar+'" /></em><span class="phbName">'+nick_name+'</span><span class="sendCount">送出<i>'+value+'</i>个</span></li>';
		return html;
	},
	setAnchorRankList:function(page){
		var _this = this;
		var init = parseInt($('#anchor15 li').length) + parseInt($('#anchor6 li').length);
		var html1 = '';
		var html2 = '';
		var btnMore2 = true;//判断是否添加【btnMore2】--加载更多
		if(init > 1){
			init --;
			$('.btnMore2').parent().remove();
		}
		//排行榜赋值
		console.log(init+_this.anchorPageSize)
		for(var j = init;j < init+_this.anchorPageSize;j++){
			var send_data = _this.receive_data_array[j];
			if(send_data == undefined || send_data == 'undefined'){
				btnMore2 = false;
				break;
			}
				
			var roomSkip = '';
			if(page == 0){
				roomSkip = _this.parseResult(send_data, 'user', 'room', 'jump');
			}else{
				roomSkip = 'http://event.m.shihou.tv/lion_module_share_temp/share?room_id='+_this.parseResult(send_data, 'user', 'room', 'id');
			}
			if(j < 5){
				html1 += _this.setAnchorRankListHTML((j+1), roomSkip, _this.parseResult(send_data, 'user', 'nick_name'), _this.parseResult(send_data, 'user', 'avatar'), _this.parseResult(send_data, 'user', 'room', 'vdo_status'), _this.resetTotal(_this.parseResult(send_data, 'total'))+'');				
			}else{
				html2 += _this.setAnchorRankListHTML((j+1), roomSkip, _this.parseResult(send_data, 'user', 'nick_name'), _this.parseResult(send_data, 'user', 'avatar'), _this.parseResult(send_data, 'user', 'room', 'vdo_status'), _this.resetTotal(_this.parseResult(send_data, 'total'))+'');
			}
		}
		if(btnMore2){
			html2 += '<li><a class="btnMore2"></a></li>';	
		}
		if($('#anchor15 li').length < 5){
			$('#anchor15').html(html1);	
		}
		if($('#anchor6 li').length > 0){
			$('#anchor6').append(html2);
			return;
		}
		$('#anchor6').html(html2);
	},
	setAnchorRankListHTML: function(index, skipRoom, nick_name, avatar, kaibo_status, value){
		var html = '';
		var li_class = '';
		if(kaibo_status == 1)
			li_class = 'class="living"';
		html = '<li '+li_class+'><a href="'+skipRoom+'"><img src="'+avatar+'"><span>'+nick_name+'</span><span>'+value+'</span><em></em></a></li>';
		return html;
	},
	resetTotal: function(total){
		return total;
		if(total == undefined)
			return 0;
		return parseInt(total/6);
	},
	sort: function(arr){
		var len = arr.length;
		for (var i = 0; i < len; i++) {
	        for (var j = 0; j < len - 1 - i; j++) {
	        	var total1 = this.parseResult(arr[j], 'total');
	        	var total2 = this.parseResult(arr[j+1], 'total');
	            if (total1 < total2) {        //相邻元素两两对比
	                var temp = arr[j+1];        //元素交换
	                arr[j+1] = arr[j];
	                arr[j] = temp;
	            }
	        }
	    }
	    for (var i = 0; i < len; i++) {
	        for (var j = 0; j < len - 1 - i; j++) {
	        	var kaibo_status1 = this.parseResult(arr[j], 'user', 'room', 'vdo_status');
	        	var kaibo_status2 = this.parseResult(arr[j+1], 'user', 'room', 'vdo_status')
	            if (kaibo_status1 < kaibo_status2) {        //相邻元素两两对比
	                var temp = arr[j+1];        //元素交换
	                arr[j+1] = arr[j];
	                arr[j] = temp;
	            }
	        }
	    }
	    return arr;
	},
	//获取url中的参数
    getUrlParam: function(name, url) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        url = url || window.location.search.substr(1).match(reg);  //匹配目标参数
        if (url != null) return unescape(url[2]); return null; //返回参数值
    },
    getUrlParams: function(){
    	var url = window.location.href;  //匹配目标参数
    	if(url != null)
    		return url.substr(url.indexOf("?"), url.length);
    },
    parseResult:function(){//解析结果， 如果不存在则返回''
		var size = arguments.length;
		if(size == 0){
			return '';
		}
		var result = arguments[0];
		for(var i = 1; i < size; i++){
			if(result == undefined){
				return '';
			}
			var argu = arguments[i];
			if(i == size){
				return argu;
			}
			result = result[argu];
		}
		return result;
	},
	requestRemoteDataJson: function(url, data, callback, asyn){
		this.logParameter(url, data);
		$.ajax({
	        url: url,
	        type: 'GET',
	        async: asyn,
	        cache: false, 
	        dataType: "json",
	        data: data,
	        success: callback,
	        error: function (data) {
	        	var status = data.status;
	        	console.log(url + "获取数据失败!");
	        }
	    });
	},
	requestRemotePostDataJson: function(url, data, callback, asyn){
		//this.logURL(url);
		$.ajax({
	        url: url,
	        type: 'POST',
	        async: asyn,
	        cache: false, 
	        dataType: "json",
	        data: data,
	        success: callback,
	        error: function (data) {
	        	var status = data.status;
	        	console.log(url + "获取数据失败!");
	        }
	    });
	},
	log:function(data){
		console.log(data);
	},
	logURL:function(url){
		console.log("url --> "+url);
	},
	logParameter:function(data){
		console.log("parameter --> "+JSON.stringify(data));
	},
	logParameter:function(args, data){
		console.log(args+" --> "+JSON.stringify(data));
	},
	logResult:function(result){
		console.log("result --> "+JSON.stringify(result));
	}
}
